import { defineStore } from "pinia"
import { getShopList, createShop, updateShop, deleteShops } from "@/api/storeMg/shop"

export const useStoreMgShopStore = defineStore('storeMgShop', {
    state: () => {
        return {
            list: [],
            total: 0
        }
    },
    actions: {
        // 获取用户管理页面列表
        getPageTableData(data) {
            return new Promise((resolve, reject) => {
                getShopList(data).then(res => {
                    this.list = res.data.list
                    this.total = res.data.total
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        // 新建、编辑
        createAndUpdateTableData(data) {
            // 深拷贝 可以是对象、数组、Map、Set、TypedArray
            let form = structuredClone(data)
            return form?.id ? this.updateTableData(form) : this.createTableData(form)
        },
        // 新增数据
        createTableData(form) {
            return new Promise((resolve, reject) => {
                createShop(form).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        // 修改数据
        updateTableData(form) {
            return new Promise((resolve, reject) => {
                updateShop(form).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        // 删除数据
        deletePageTableData(data) {
            let ids = data.map(item => item.id)
            return new Promise((resolve, reject) => {
                deleteShops(ids).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        }
    }
})